/*
 * This file is part of Space Invaders: Rehash
 * Copyright (C)2007 Vicent Marti (tanoku@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <math.h>
#include <vector>

#include "SDL.h"
#include "SDL_opengl.h"

#include <GL/gl.h>
#include <GL/glu.h>

#include "main.h"
#include "gfx.h"
#include "sprites.h"

extern GLuint g_Textures[TEXTURE_COUNT];

// TODO: why the fuck isn't this in gfx.cpp?
void glDrawSprite2D( GLuint sprite, int id, int rows, int cols, int x, int y, float size,
					float rotation, float r, float g, float b, float a)
{

	glLoadIdentity();
	glScreenBlend();
	glTranslatef( (GLfloat)x, (GLfloat)y, 0 );
	glRotatef( rotation, 0.0f, 0.0f, 1.0f );
	
	float uv_width = 1.0f / (float)cols;
	float uv_base_x = uv_width*(id%cols);
	float uv_base_y = uv_width*( rows - 1 - (int)(id/cols));
	
	glColor4f( r, g, b, a );
	glEnable( GL_BLEND );
	glBindTexture(GL_TEXTURE_2D, sprite);
	
	glBegin( GL_QUADS );
		glTexCoord2f( uv_base_x, uv_base_y + uv_width ); 
		glVertex3f( 0.0f, 0.0f,	0.0f );
	
		glTexCoord2f( uv_base_x + uv_width, uv_base_y + uv_width ); 
		glVertex3f( (GLfloat)size, 0.0f, 0.0f );
	
		glTexCoord2f( uv_base_x + uv_width, uv_base_y ); 
		glVertex3f( (GLfloat)size,	(GLfloat)size, 0.0f );
	
		glTexCoord2f( uv_base_x, uv_base_y ); 
		glVertex3f( 0.0f, (GLfloat)size, 0.0f );
	glEnd();
	
	glBindTexture (GL_TEXTURE_2D, 0);
	glAlphaBlend();
	glDisable(GL_BLEND);
	
}

void Explosion::render()
{
	if ( m_dead )
		return;
	
	float life = (float)m_lifeTimer.get_ticks()/(float)m_timeToLive;
	float frames = (float)m_currentFrame/(float)m_maxFrames;
	
	if ( life > 1.0f )
	{
//		printf("Sprite dead!\n");
		m_dead = true;
		return;
	}
	
	if ( life > frames )
		m_currentFrame++;
	
	glDrawSprite2D( g_Textures[m_sprite], m_currentFrame, m_rows, m_cols, m_x - m_size/2, 
				   m_y - m_size/2, (float)m_size, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f);
}

void Smoke::render()
{
	if ( m_dead )
		return;

	float life = (float)m_lifeTimer.get_ticks()/(float)m_timeToLive;
	float frames = (float)m_currentFrame/(float)m_maxFrames;

	if ( life > 1.0f )
	{
		printf("Sprite dead!\n");
		m_dead = true;
		return;
	}

	if ( life > frames )
	{
		m_currentFrame++;
	}

	float alpha = m_fadeOut?1.0f-life:1.0f;
	
	glDrawSprite2D( g_Textures[m_sprite], m_currentFrame, m_rows, m_cols, m_x - m_size/2, 
				   m_y - m_size/2, (float)m_size, 0.0f, 1, 1, 1, alpha);
}

void SmokeTrail::render()
{
	if ( m_dead )
		return;
	
	float life = (float)m_lifeTimer.get_ticks()/(float)m_timeToLive;

	if ( life > 1.0f )
	{
//		printf("Smoke trail over.\n");
		m_dead = true;
		return;
	}
	
	for ( int i = 0; i<4; ++i )
	{
		glDrawSprite2D( g_Textures[7], 4, 4, 4, m_x, 
				   m_y + i*32, 32, 0.0f, 1, 1, 1, 1.0);
	}

}
